<?php
/**
 * Created by PhpStorm.
 * User: zhangligang
 * Date: 17/6/21
 * Time: 上午12:07
 */

namespace App\Http\Controllers;


use App\Http\Models\Role;
use App\Http\Models\Rule;
use App\Http\Models\User;
use App\Http\Presenter\UserPresenter;
use App\Http\Repository\UserRepository;
use App\Http\Service\UserService;
use Illuminate\Http\Request as HttpRequest;

class UserController extends BaseController
{

    /**
     * @param UserPresenter $presenter
     * @param UserRepository $repository
     * @param UserService $service
     */
    public function __construct(UserPresenter $presenter, UserRepository $repository, UserService $service)
    {

        $this->view = ['activeFirst' => 'user', 'activeSecond' => 'index'];

        parent::__construct($presenter, $repository, $service);

    }

    public function index()
    {

        $userList = User::all(['id']);

        $this->view['activeSecond'] = 'index';

        return view('user.index', array_merge(['userList' => $userList], $this->view));
    }

    public function create()
    {

        $user = new User();

        $this->view['activeSecond'] = 'index';

        return view('user.edit', array_merge(['user' => $user], $this->view));
    }

    public function update($id)
    {

        $user = User::query()->find($id);

        $this->view['activeSecond'] = 'index';

        return view('user.edit', array_merge(['user' => $user], $this->view));
    }

    public function save(HttpRequest $request)
    {

        $userArray = $request->all();

        if (empty($userArray['id'])) {

            $user = new User();

        } else {

            $user = User::query()->find($userArray['id']);
        }

        $user->username = empty($userArray['username']) ? '' : $userArray['username'];

        $user->password = empty($userArray['password']) ? '' : $userArray['password'];

        $user->save();

        return redirect('user.html');
    }

    public function delete(HttpRequest $request)
    {

        $id = (int)$request->get('id', 1);

        $code = 200;

        $message = '删除成功';

        if (User::query()->where('id', '=', $id)->delete() === false) {

            $code = 404;

            $message = '删除失败,请重试';
        }

        return $this->send($code, $message);
    }

    public function set(HttpRequest $request)
    {

    }

    public function role()
    {

        $roleList = Role::all(['id', 'name']);

        $this->view['activeSecond'] = 'role';

        return view('user.role', array_merge(['roleList' => $roleList], $this->view));
    }

    public function roleCreate()
    {

        $role = new Role();

        $this->view['activeSecond'] = 'role';

        return view('user.role-edit', array_merge(['role' => $role], $this->view));
    }

    public function roleUpdate($id)
    {

        $role = Role::query()->find($id);

        $this->view['activeSecond'] = 'role';

        return view('user.role-edit', array_merge(['role' => $role], $this->view));
    }

    public function roleSave(HttpRequest $request)
    {

    }

    public function roleDelete(HttpRequest $request)
    {

    }

    public function roleSet(HttpRequest $request)
    {

    }

    public function rule()
    {

        $ruleList = Rule::all(['id', 'name']);

        $this->view['activeSecond'] = 'rule';

        return view('user.rule', array_merge(['ruleList' => $ruleList], $this->view));
    }

    public function ruleCreate()
    {

        $rule = new Rule();

        $this->view['activeSecond'] = 'rule';

        return view('user.rule-edit', array_merge(['rule' => $rule], $this->view));
    }

    public function ruleUpdate($id)
    {

        $rule = Rule::query()->find($id);

        $this->view['activeSecond'] = 'rule';

        return view('user.rule-edit', array_merge(['rule' => $rule], $this->view));
    }

    public function ruleSave(HttpRequest $request)
    {

    }

    public function ruleDelete(HttpRequest $request)
    {

    }

    public function ruleSet(HttpRequest $request)
    {

    }
}